<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null : 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item label="微信商户一级类目代码" :labelCol="{ span: 8 }" :wrapperCol="{ span: 12, offset: 1 }">
                <a-input placeholder="请输入微信商户一级类目代码" v-model="queryParams.wctFstNo" />
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item label="微信商户一级类目描述" :labelCol="{ span: 8 }" :wrapperCol="{ span: 12, offset: 1 }">
                <a-input placeholder="请输入微信商户一级类目描述" v-model="queryParams.wctFstName" />
              </a-form-item>
            </a-col>
          </a-row>

        </div>
        <div style="text-align: center">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>

    <div>
      <div class="operator">
        <a-button v-hasPermission="['wechatFirst:add']" ghost type="primary" @click="addWechatFirst">新增</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo" :columns="columns" :rowKey="record => record.id" :dataSource="dataSource"
        :pagination="pagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :scroll="{ x: 1186 }"
        @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{ text }}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{ text }}</p>
          </a-popover>
        </template>
        <template slot="wechatFirstOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['wechatFirst:edit']" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
            @click="editWechatFirst(record)" title="修改"></a-icon>&nbsp;
          <a-icon v-hasPermission="['wechatFirst:cancel']" type="poweroff" twoToneColor="#4a9ff5"
            @click="cancelWechatFirst(record)" title="注销"></a-icon>&nbsp;
          <a-icon v-hasPermission="['wechatFirst:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
            @click="viewWechatFirst(record)" title="查看"></a-icon>
          <a-badge v-hasNoPermission="['wechatFirst:edit', 'wechatFirst:cancel', 'wechatFirst:view']" userStatus="warning"
            text="无权限"></a-badge>
        </template>

      </a-table>

      <!-- 操作 -->
      <wechat-first-operation ref="wechatFirstOperation" @close="handleWechatFirstOperationClose"
        @success="handleWechatFirstOperationSuccess" :wechatFirstOperationVisiable="wechatFirstOperation.visiable">
      </wechat-first-operation>

    </div>
  </a-card>
</template>

<script>
import RangeDate from '@/components/datetime/RangeDate';
import { mapState } from "vuex"
import WechatFirstOperation from "./WechatFirstOperation";

export default {
  name: "WechatFirst",
  components: { WechatFirstOperation, RangeDate },
  data() {
    return {
      advanced: false,
      wechatFirstOperation: {
        visiable: false,
      },

      queryParams: {
        wctFstNo: '',
        wctFstName: '',
      },

      dataSource: [],
      sortedInfo: null,
      paginationInfo: null,
      selectedRowKeys: [],
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false,

    }
  },
  computed: {
    ...mapState({
      user: state => state.account.user,
    }),
    columns() {
      let { sortedInfo } = this
      sortedInfo = sortedInfo || {}
      let { filteredInfo } = this
      filteredInfo = filteredInfo || {}
      return [{
        title: '微信商户一级类目代码',
        dataIndex: 'wctFstNo',
        scopedSlots: { customRender: 'wctFstNo' },
      }, {
        title: '微信商户一级类目描述',
        dataIndex: 'wctFstName',
        scopedSlots: { customRender: 'wctFstName' },
      }, {
        title: '商户主体',
        dataIndex: 'wctFstType',
        customRender: (text, row, index) => {
          switch (text) {
            case '01':
              return '企业'
            case '02':
              return '个体工商户'
            case '03':
              return '事业单位'
            default:
              return text
          }
        }
      }, {
        title: '状态',
        dataIndex: 'status',
        customRender: (text, row, index) => {
          switch (text) {
            case '00':
              return '非正式'
            case '01':
              return '正式'
            case '02':
              return '停用'
            case '03':
              return '已注销'
            default:
              return text
          }
        },
      }, {
        title: '操作',
        dataIndex: 'wechatFirstOperation',
        fixed: 'right',
        width: 100,
        show: false,
        scopedSlots: { customRender: 'wechatFirstOperation' }
      }]
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleWechatFirstOperationClose() {
      this.wechatFirstOperation.visiable = false
    },
    handleWechatFirstOperationSuccess() {
      this.wechatFirstOperation.visiable = false
      this.$message.success('操作成功')
      this.search()
    },


    addWechatFirst() {
      this.wechatFirstOperation.visiable = true
    },
    viewWechatFirst(record) {
      record.operation = 'view'
      this.$refs.wechatFirstOperation.setWechatFirstFormValues(record)
      this.wechatFirstOperation.visiable = true
    },
    editWechatFirst(record) {
      record.operation = 'edit'

      if (record.status == "03") {
        this.$message.warning("已注销数据不允许修改");
      } else {
        this.$refs.wechatFirstOperation.setWechatFirstFormValues(record)
        this.wechatFirstOperation.visiable = true
      }
    },
    cancelWechatFirst(record) {
      record.operation = 'cancel'
      if (record.status == "03") {
        this.$message.warning("已注销数据不允许重复注销");
      } else {
        this.$refs.wechatFirstOperation.setWechatFirstFormValues(record)
        this.wechatFirstOperation.visiable = true
      }
    },

    search() {
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }

      let { sortedInfo, filteredInfo } = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      let params = this.queryParams

      this.fetch({
        sortField: sortField,
        sortOrder: sortOrder,
        ...params,
        ...filteredInfo
      })
    },
    reset() {
      // 取消选中
      this.selectedRowKeys = []
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      // 重置列排序规则
      this.sortedInfo = null
      // 重置查询参数
      this.queryParams = {}
      // // 清空时间选择
      // this.$refs.createTime.reset()
      this.fetch()
    },
    handleTableChange(pagination, filters, sorter) {
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }

      // 将这三个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter

      this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    fetch(params = {}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('/admin/merParam/wctFstInfo', {
        ...params
      }).then((r) => {
        let data = r.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        this.loading = false
      })
    },
  }
}
</script>

<style lang="less" scoped>
@import "../../../../../../static/less/Common";
</style>

